package com.psjj.privilege.service;

import com.psjj.privilege.dao.PrivilegeDao;
import com.psjj.privilege.po.Privilege;

import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

public class PrivilegeService {
    private PrivilegeDao privilegeDao = new PrivilegeDao();
    //加载权限数据并关联角色拥有的权限数据
    public List<Privilege> queryPrivilegesJoinRoleHasPrivilege(int rid) {
        List<Privilege> list = null;
        try {
            list = privilegeDao.queryPrivileges();
            //遍历当前角色所拥有的权限，将权限状态设置为checked为true
            list.forEach(privilege->{
                try {
                    int privilegeId = privilege.getMid();
                    boolean tag = privilegeDao.HasPrivilegeIdAndRid(privilegeId,rid);
                    if(tag){
                        privilege.setChecked(true);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            });

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    //维护角色id和权限id的关系
    public void saveRoleIdAndMids(int rid, String midsStr) {
        try {
            //根据角色id清空数据源原来的维护
            privilegeDao.clearMidsByRoleId(rid);
            //维护新的rid与mids
            saveMidsByRid(rid,midsStr);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //将角色id和权限id保存到数据库
    private void saveMidsByRid(int rid, String midsStr) {

        try {
            String[] mids = null;
            if(midsStr!=null && !midsStr.equals("")){
                mids = midsStr.split(",");
            }
            if(mids!=null  && mids.length>0) {
                for(String mid : mids){
                    int privilegeid = Integer.parseInt(mid);
                    privilegeDao.addPrivilegeIdAndRoleId(privilegeid,rid);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //加载顶级权限数据并关联二级权限数据
    public List<Privilege> queryTopPrivileges() {
        //定义所有权限数据
        final  List<Privilege> privileges;
        //定义顶级权限数据并关联二级权限数据
        List<Privilege> topPrivileges = null;
        try {
            //查询所有权限数据
            privileges = privilegeDao.queryPrivileges();
            //数据-流-过滤流顶级权限数据-
            topPrivileges = privileges.stream().filter(privilege -> {
                Integer pid = privilege.getPid();
                if(pid==null || pid==0){
                    return true;
                } else {
                    return false;
                }
            }).map(privilege -> {
                //顶级权限的mid就是二级权限pid
                Integer pid = privilege.getMid();
                //根据pid查询二级权限
                List<Privilege> twoPrivileges = searchTowPrivilegesByPid(privileges,pid);
                //为当前权限设置二级权限
                privilege.setChildren(twoPrivileges);
                return privilege;
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return topPrivileges;
    }
    //根据pid查询二级权限
    private List<Privilege> searchTowPrivilegesByPid(List<Privilege> privileges, Integer pid) {
        return privileges.stream().filter(privilege -> {
            Integer pid1 = privilege.getPid();
            if(pid1==pid){
                return true;
            }else {
                return false;
            }
        }).collect(Collectors.toList());
    }

    public List<String> queryUrls() {
        List<String> urls = null;
        try {
            urls = privilegeDao.urls();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return urls;
    }
}
